/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Hogar;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class SelectAlarm extends Form implements CommandListener, ItemStateListener{
private StringItem msg;
private ChoiceGroup elec;
private TextField txt;
private Ticker tick;
private Command atras,aceptar;
private int numtxt;
private boolean estado;
private int habitacionActiva = 0;
private Hogar midlet;
public SelectAlarm(Hogar midlet){
super("");
this.midlet = midlet; //Guardo la referencia del MIDlet
numtxt = -1; //Indice del TextField en el formulario
tick = new Ticker("");
txt = new TextField("Introduzca clave","",10,TextField.PASSWORD);
String opc[] = {"No","Si"};
elec = new ChoiceGroup("",Choice.EXCLUSIVE,opc,null);
elec.setSelectedIndex(0,true);
atras = new Command("Atras",Command.BACK,1);
aceptar = new Command("Aceptar",Command.OK,1);
msg = new StringItem("","");
this.setTicker(tick);
this.append(msg);
this.append(elec);
this.addCommand(atras);
this.setItemStateListener(this);
this.setCommandListener(this);
}
public void itemStateChanged(Item item){
if (item == elec){//Miro qué opción he elegido
if (elec.getSelectedIndex() == 1){
numtxt = this.append(txt);
this.addCommand(aceptar);
}
else if (numtxt != -1){
this.delete(numtxt);
numtxt = -1;
this.removeCommand(aceptar);
}
}
}
public void commandAction(Command c, Displayable d){
if (c == aceptar){
//Comprobar contraseña
estado = !estado;
midlet.edificio.setAlarHab(estado);
midlet.setAlarma();
}
if (numtxt != -1){
this.delete(numtxt);
numtxt = -1;
this.removeCommand(aceptar);
}
this.txt.setString("");
this.elec.setSelectedIndex(0,true);
if (c == atras){
midlet.setAlarma();
}
}
public void setEstado(boolean est){
estado = est;
if (estado){
msg.setText("¿Desea desactivar la alarma?");
tick.setString("Alarma activada");
}else{
msg.setText("¿Desea activar la alarma?");
tick.setString("Alarma desactivada");
}
}
public boolean getestado(){
return estado;
}
public void setHabitacion(int n){
habitacionActiva = n;
}
}
